package com.openstack4j.qx.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.alibaba.fastjson.JSON;
import com.openstack4j.qx.LoadBalanceController;
import com.openstack4j.qx.imp.LoadBalanceControllerImpl;
import com.openstack4j.qx.pojo.ApplyLoadBalanceResp;
import com.openstack4j.qx.pojo.BaseResp;
import com.openstack4j.qx.pojo.LoadBalanceTemplate;

/**
 * ClassName:LoadBalanceRest
 * Function: 负载均衡操作rest接口
 * Create Date:  2016年12月6日:下午6:03:53
 * Author:       yuanzhijin
 * @see 	 
 */

@Path("/LoadBalanceRest")
public class LoadBalanceRest {
	
	LoadBalanceController  loadBalanceController = new LoadBalanceControllerImpl();
	
	@POST
	@Path("/ApplyIPReq")
	@Produces(MediaType.APPLICATION_JSON)
	public  BaseResp applyLoadBalance(@Context HttpServletRequest request){
		ApplyLoadBalanceResp resp = new ApplyLoadBalanceResp();
		try {
			String loadBalanceTemplate =request.getParameter("loadBalanceTemplate");
			
			LoadBalanceTemplate loadBalance= JSON.parseObject(loadBalanceTemplate, LoadBalanceTemplate.class);
			
			resp = loadBalanceController.applyLoadBalance(loadBalance);
			
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}
	
	
	@POST
	@Path("/CancellLoadBalanceReq")
	@Produces(MediaType.APPLICATION_JSON)
	public  BaseResp cancleLoadBalance(@Context HttpServletRequest request){
		BaseResp resp = new BaseResp();
		
		try {
			String lbID =request.getParameter("loadBalanceTemplate");
			
			resp = loadBalanceController.cancelLoadBalance(lbID);
			
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	} 
	
	
	@POST
	@Path("/ModifyLoadBalanceReq")
	@Produces(MediaType.APPLICATION_JSON)
	public  BaseResp modifyLoadBalance(@Context HttpServletRequest request){
		BaseResp resp = new BaseResp();
		try {
			String loadBalanceTemplate =request.getParameter("loadBalanceTemplate");
			
			LoadBalanceTemplate loadBalance= JSON.parseObject(loadBalanceTemplate, LoadBalanceTemplate.class);
			
			resp = loadBalanceController.modifyLoadBalance(loadBalance);
			
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	} 
	

}

